﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using com.SFA.Server.Entity;
using Jumpton.Data;
using Jumpton.Persistent;

namespace BusinessRule.WareHouse
{
    public class WarehouseVoucherManager : BusinessLogic
    {
        public WAREHOUSE_VOUCHER_HEADER Load(decimal ID, decimal DomainID,out global::System.Data.DataTable lines)
        {
            var header = (WAREHOUSE_VOUCHER_HEADER)ObjectRow.Load(typeof(WAREHOUSE_VOUCHER_HEADER), "ID=" + ID);
            if (header != null)
                lines = SqlData.OpenSql(@"SELECT T1.ID,VOUCHER_LINE_CODE AS CODE,QUANTITY,SHORT_DESCRIPTION AS ProductName,PRODUCT_ID AS PRODUCTID,WARE_ID AS WAREHOUSE,T1.PIECE_PRICE,LINE_AMOUNT,UNIT AS UOM
             FROM WAREHOUSE_VOUCHER_LINE T1,PRODUCTS T2 WHERE T1.PRODUCT_ID=T2.ID AND T1.VOUCHER_CODE='" + header.VOUCHERCODE + "' AND T1.DOMAIN_ID=" + DomainID).Tables[0];
            else
                lines = null;
            return header;
        }
        /// <summary>
        /// 判断入库单号是否存在
        /// </summary>
        /// <param name="DomainID"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public Boolean ExistVoucherCode(decimal DomainID, String Code)
        {
            object value = SqlData.ExecuteScalar(string.Format("SELECT COUNT(*) from WAREHOUSE_VOUCHER_HEADER WHERE ACTIVE=1 AND DOMAIN_ID={0} AND VOUCHER_CODE='{1}'",
                DomainID,Code));
            return !0.Equals(value);
        }
        public Boolean Save(WAREHOUSE_VOUCHER_HEADER header,WAREHOUSE_VOUCHER_LINE[] lines)
        {
             if(header == null || lines == null)
                return false;
             if (lines.Length == 0 && header.State == global::System.Data.DataRowState.Added)
             {
                 return false;
             }
            //判断该入库单号是否存在，如果存在则不允许添加
           if (header.State == global::System.Data.DataRowState.Added && ExistVoucherCode(header.DOMAINID, header.VOUCHERCODE))
             {
                 throw new Exception("存在相同的的入库单号");
             }
            header.Connection.BeginTransaction(null);
            header.CREATETIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            header.LASTUPDATETIME = header.CREATETIME;
            try
            {
                InventoryManager manager = new InventoryManager();
                foreach (var line in lines)
                {

                    line.ACTIVE = true;
                    line.DOMAINID = header.DOMAINID;
                    line.LASTUPDATETIME = header.LASTUPDATETIME;
                    line.LASTUPDATER = header.LASTUPDATER;
                    line.CREATETIME = header.CREATETIME;
                    line.VOUCHERCODE = header.VOUCHERCODE;
                    line.Connection.Transaction = header.Connection.Transaction;
                    line.Save();
                    //更新库存数据表
                    var inventory = manager.Load(line.PRODUCTID, line.WAREID,line.UNIT);
                    if (inventory == null)
                    {
                        inventory = new INVENTORY();
                        inventory.PRODUCTID = line.PRODUCTID;
                        inventory.PIECEPRICE = line.PIECEPRICE;
                        inventory.ACTIVE = true;
                        inventory.CREATETIME = line.CREATETIME;
                        inventory.DOMAINID = line.DOMAINID;
                        inventory.WAREID = line.WAREID;
                        inventory.QUANTITY = 0;
                        inventory.UNIT = line.UNIT;
                    }
                   
                    inventory.LASTUPDATER = line.LASTUPDATER;
                    inventory.LASTUPDATETIME = line.LASTUPDATETIME;
                    inventory.QUANTITY += line.QUANTITY * line.CONVERSIONFACTOR;
                    inventory.Connection.Transaction = header.Connection.Transaction;
                    inventory.Save();
                }

                header.Save();
                header.Connection.CommitTransaction();
            }
            catch(Exception err)
            {
                header.Connection.RollbackTransaction();
                return false;
            }
            return true;
        }
    }
}
